<template>
  <div>
    <el-form :model="form" inline>
      <el-form-item label="订单号:">
        <el-input v-model="form.orderNumberCode" clearable />
      </el-form-item>
      <el-form-item label="买家退回运单号:">
        <el-input v-model="form.trackingNumbers" clearable />
      </el-form-item>
      <el-form-item label="订单来源:">
        <el-select v-model="form.productTypeId" clearable>
          <el-option :label="item.value" :value="item.key" v-for="item in store.productReturnedStorageSearchDicArr"
            :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="买家申请退货时间:">
        <el-date-picker v-model="times" type="datetimerange" start-placeholder="开始时间" value-format="x"
          end-placeholder="结束时间" time-format="A hh:mm:ss" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column label="订单来源">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="订单号" width="180">
        <template #default="scope">{{ scope.row.orderNumberCode }}</template>
      </el-table-column>
      <el-table-column label="商品SKU" width="180">
        <template #default="scope">{{ scope.row.skuInfo }}</template>
      </el-table-column>
      <el-table-column label="退货数量" width="180">
        <template #default="scope">{{ scope.row.storageNum }}</template>
      </el-table-column>
      <el-table-column label="采购订单号" width="180">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="提交时间" width="180">
        <template #default="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="所在库位">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="状态">
        <template #default="scope">{{ scope.row.storageStatus == 1 ? '已入库' : '未入库' }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="printProductPurchaseStorageBtn(scope.row)">
            打印入库单
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
</template>

<script setup>
import { ref, onBeforeMount, onBeforeUnmount } from 'vue'
import { productReturnedStorageList, productReturnedStoragecreatCode } from '@/api/index'
import { useAlertsStore } from '@/store/index'
import { ElMessage } from 'element-plus'
const store = useAlertsStore()
// do not use same name with ref
const form = ref({
  returnedApplyTimeStart: '',
  returnedApplyTimeEnd: '',
  productTypeId: 0,
  orderNumberCode: '',
  trackingNumbers: '',
  storageStatus: 1,
  pageSize: 10,
  pageNum: 1
})
const tableData = ref([])
onBeforeMount(() => {
  getList()
  store.productReturnedStorageSearchDicBtn()
})
const times = ref('')
const onSubmit = () => {
  console.log('submit!')
  getList()
}
const getList = () => {
  if (times.value) {
    form.value.returnedApplyTimeStart = times.value[0]
    form.value.returnedApplyTimeEnd = times.value[1]
  } else {
    form.value.returnedApplyTimeStart = ''
    form.value.returnedApplyTimeEnd = ''
  }
  productReturnedStorageList(form.value).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}

// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const valA4 = ref([])
const taketable = ref({ areaShop: '', storagelocation: '', num: '', batchNum: '', pdaCode: '' })
const printProductPurchaseStorageBtn = (row) => {
  productReturnedStoragecreatCode({ returnedStorageId: row.id }).then(res => {
    ElMessage.success('操作成功')
    const { data: { data } } = res
    // const a =
    // a.forEach(element => {
    taketable.value.areaShop = data.warehouseName
    taketable.value.storagelocation = JSON.parse(data.storageWarehouseLocation).warehouseLocationData[0].name
    taketable.value.num = data.storageNum
    taketable.value.skuInfos = data.skuInfo
    taketable.value.batchNum = data.inboundBatchesCode
    // })
    valA4.value.push(taketable.value)
    getList()
    console.log('tableData.value')
    console.log(valA4.value)
    if (localStorage.getItem('PrinterSetListRK')) {
      const lists = JSON.parse(localStorage.getItem('PrinterSetListRK'))
      console.log(lists[0].PrinterList)
      const val = { cmd: 'print', requestID: '123458976', version: '1.0', task: { taskID: (new Date().getTime() + ''), preview: false, printer: lists[0].PrinterList, notifyMode: 'allInOne', previewType: 'pdf', documents: [{ documentID: '0123456789', contents: [{ data: { width: '210', height: '297', padding: '2', fontSize: '8', left: '2', padcode: data.pdaCode, skuInfo: valA4.value, barcodeHeight: '33' }, templateURL: 'http://yitaoweb.sc.kuaidi600.com/moban/a4.xml' }] }] } }
      store.socket.send(JSON.stringify(val))
    } else {
      ElMessage.error('先设置打印机')
    }
  })
}
onBeforeMount(() => {
  doConnect()
})
// 销毁
onBeforeUnmount(() => {
  console.log('销毁')
  store.destroyed()
})
const doConnect = () => {
  console.log('d打开了不')
  store.socket = new WebSocket('ws://localhost:13528')
  // 打开Socket
  store.socket.onopen = function (event) {
    // 监听消息
    store.socket.onmessage = function (event) {
      // localStorage.setItem('printerproperties', event.data)
    }
    // 监听Socket的关闭
    store.socket.onclose = function (event) {
      // console.log('Client notified socket has closed', event)
    }
  }
}
</script>

<style lang='less' scoped></style>
